package org.jeecg.modules.activiti.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.*;
import org.jeecg.modules.activiti.entity.ActZTask;

import java.util.List;
import java.util.Map;

/**
 * @Description: 待办任务扩展表
 * @Author: qixin-boot
 * @Date:   2022-02-08
 * @Version: V1.0
 */
public interface ActZTaskMapper extends BaseMapper<ActZTask> {

    // 更新待办任务表的 state、node_state、audit_time
    @Update("update act_z_task set state = #{state}, node_state = #{nodeState}, audit_time = now()," +
            " update_time = now(), audit_comment = #{comment}  where id = #{id}")
    void updateState(@Param("id") String id, @Param("state") Integer state, @Param("nodeState") Integer nodeState ,
                     @Param("comment") String comment);

    // 更新待办任务表的 state、node_state、audit_time
    @Update("update act_z_task set state = #{state}, node_state = #{nodeState}, audit_time = now()," +
            " update_time = now(), audit_comment = #{comment}  where task_id = #{taskId}")
    void updateStateByTaskId(@Param("taskId") String taskId, @Param("state") Integer state, @Param("nodeState") Integer nodeState ,
                     @Param("comment") String comment);

    // 获取流程实例待办任务Id
    @Select("select task_id from act_z_task where proc_inst_id = #{procInstId} and node_state = 0 limit 1")
    String getTaskId(@Param("procInstId")String procInstId);

    //删除运行时任务节点表-待办任务--待办任务id
    @Delete("delete from act_ru_task where id_ = #{taskId}")
    void deleteRuTask(@Param("taskId")String taskId);

    //删除历史流程任务表的任务-待办任务id
    @Delete("delete from act_hi_taskinst where id_ = #{taskId}")
    void deleteHiTask(@Param("taskId")String taskId);

    //获取已办任务里的最新的任务id(当前流程实例,同时end_time_不为空,task_id不为空)
    @Select("select * from act_hi_taskinst where end_time_ is not null and proc_inst_id_ = #{procInstId} order by start_time_ desc limit 1 ")
    Map<String,Object> selectLastHiTask(@Param("procInstId")String procInstId);

    //获取已办任务里的最新的任务id(再前一个已办任务id)(当前流程实例,同时end_time_不为空,task_id不为空)
    @Select("select * from act_hi_taskinst where end_time_ is not null and proc_inst_id_ = #{procInstId} and id_ != #{lastTaskId} order by start_time_ desc limit 1 ")
    Map<String,Object> selectLast2HiTask(@Param("procInstId")String procInstId,@Param("lastTaskId") String lastTaskId);

    //获取已办任务里特征表中的最新的数据:lastTaskId,procInstId
    @Select("select * from act_hi_identitylink where task_id_ = #{lastTaskId} and proc_inst_id_ is null ")
    List<Map<String, Object>> selectLastHiIdentity(@Param("lastTaskId") String lastTaskId);

    //更新历史任务表已办任务(act_hi_taskinst)字段:id_(已办任务id)::结束时间清空,持续时间清空,删除理由清空
    @Update("update act_hi_taskinst set end_time_ = null,duration_ = null,delete_reason_ = null where id_ = #{lastTaskId} ")
    void updateActHiTask(@Param("lastTaskId") String lastTaskId);

    //删除历史流程人员表-待办任务(act_hi_identitylink):task_id_(当前任务id)::节点实例id
    @Delete("delete from act_hi_identitylink where task_id_ = #{taskId} ")
    void deleteActHiIdentityByTaskId(@Param("taskId") String taskId);

    //删除历史流程人员表-已办任务(act_hi_identitylink):task_id_(已办任务id)::流程实例id
    @Delete("delete from act_hi_identitylink where task_id_ = #{lastTaskId} and proc_inst_id_ = #{procInstId}  ")
    void deleteActHiIdentity(@Param("lastTaskId") String lastTaskId, @Param("procInstId") String procInstId);

    //删除历史意见表-意见(act_hi_comment):task_id_(已办任务id)
    @Delete("delete from act_hi_comment where task_id_ = #{lastTaskId} ")
    void deleteActHiComment(@Param("lastTaskId") String lastTaskId);

    //删除运行时流程人员表-节点参与者(act_ru_identitylink):task_id_(待办任务id)
    @Delete("delete from act_ru_identitylink where task_id_ = #{taskId} ")
    void deleteActRuIdentity(@Param("taskId") String taskId);

    //将查询出来的已办任务(act_hi_taskinst)数据,插入到待办任务表(act_ru_task)
    @Insert("insert into act_ru_task (ID_,REV_,EXECUTION_ID_,PROC_INST_ID_,PROC_DEF_ID_,NAME_,TASK_DEF_KEY_,OWNER_,ASSIGNEE_,PRIORITY_,CREATE_TIME_,SUSPENSION_STATE_) " +
            "values (#{ID_},'1',#{EXECUTION_ID_},#{PROC_INST_ID_},#{PROC_DEF_ID_},#{NAME_},#{TASK_DEF_KEY_},#{OWNER_},#{ASSIGNEE_},#{PRIORITY_},#{START_TIME_},'1') ")
    void insertActRuTask(Map<String, Object> actHiTaskMap);

    //将上一个节点已办的任务特征表(act_hi_identitylink),插入到待办的特征表(act_ru_identitylink)
    @Insert("insert into act_ru_identitylink (ID_,REV_,TYPE_,USER_ID_,TASK_ID_) values (#{ID_},'1','candidate',#{USER_ID_},#{TASK_ID_}) ")
    void insertActRuIdentity(Map<String, Object> actHiIdentityMap);

    //删除历史节点表(act_hi_actinst)中,(已办任务id,并且时间大于任务开始时间的所有记录)
    @Delete("delete from act_hi_actinst where proc_inst_id_ = #{procInstId} and start_time_ > #{startTime}  ")
    void deleteActHiActinst(@Param("procInstId") String procInstId, @Param("startTime") String startTime);

    //查询任务的开始时间
    @Select("select START_TIME_,DELETE_REASON_,PROC_INST_ID_ from act_hi_taskinst where id_ = #{lastTaskId}")
    Map<String,Object> selectActHiActinst(@Param("lastTaskId") String lastTaskId);

    //更新历史节点表(act_hi_taskinst)中,字段:task_id_(已办任务id),将结束日期清空,持续时间清空
    @Update("update act_hi_taskinst set end_time_ = null,duration_ = null,delete_reason_ = 'canceled-' where id_ = #{lastTaskId} ")
    void updateActHiTaskinst(@Param("lastTaskId") String lastTaskId);

    //更新运行时流程执行实例表(act_ru_execution)字段:act_id_为上一节点任务的task_def_key_值
    @Update("update act_ru_execution set act_id_ = #{taskDefKey} where proc_inst_id_ = #{procInstId} ")
    void updateActRuExecution(@Param("procInstId") String procInstId,@Param("taskDefKey") String taskDefKey);

    // 更新业务表单据状态
    @Update("update ${tableName} set state = #{state} ,update_time = now() where id = #{tableId}")
    void updateBusinessState(@Param("tableName")String tableName, @Param("tableId")String tableId, @Param("state")String actStatus);

    // 根据流程实例Id更新待办任务表状态
    @Update("update act_z_task set state = #{state}, update_time = now() where proc_inst_id = #{procInstId}")
    void updateActZTaskState(@Param("state") Integer state, @Param("procInstId") String procInstId);

    // 删除流程实例大于当前的序号的待办任务
    @Delete("delete from act_z_task where proc_inst_id = #{procInstId} and serial > #{serial}")
    void delActZTasckByProcInstId(@Param("serial") Integer serial, @Param("procInstId") String procInstId);

    //根据任务Id获取历史流程任务表信息(act_hi_taskinst)
    @Select("select * from act_hi_taskinst where id_ = #{id} ")
    Map<String,Object> selectHiTaskById(@Param("id") String id);

    //根据流程实例Id-删除历史节点表的 保留当前节点记录taskId
    @Delete("delete from act_hi_taskinst where proc_inst_id_ = #{procInstId} and id_ <> #{id}")
    void delHiTaskByProcInstId(@Param("procInstId") String procInstId, @Param("id") String id);

    //删除历史流程人员表-发起人撤回时(act_hi_identitylink):ProcInstId:节点实例id
    @Delete("delete from act_hi_identitylink where task_id_ in (SELECT task_id from act_z_task " +
            " where proc_inst_id = #{procInstId} and serial > 1)")
    void delActHiIdentityByProcInstId(@Param("procInstId") String procInstId);

    //删除历史意见表-意见(act_hi_comment):procInstId(流程实例id)
    @Delete("delete from act_hi_comment where proc_inst_id_ = #{procInstId} ")
    void delActHiCommentByProcInstId(@Param("procInstId") String procInstId);

    //删除运行时流程人员表-节点参与者(act_ru_identitylink):ProcInstId(流程实例id)
    @Delete("delete from act_ru_identitylink where proc_inst_id_ = #{procInstId} and type_ != 'starter'")
    void delActRuIdentityByProcInstId(@Param("procInstId") String procInstId);

    /**
     * @Description: 根报销单据id获取任务表序号最大的审批人信息
     * @author: CYS
     * @date: 2022/3/1 10:35
     * @param procInstId:
     * @Return:
     */
    @Select("select * from act_z_task where proc_inst_id=#{procInstId}  order by serial desc limit 1")
    ActZTask getActZTaskByTableId(@Param("procInstId") String procInstId);

    /**
    * @Description: 根据id查询历史参与人表
    * @author: CYS
    * @date: 2022/4/1 18:17
    * @param procInstId:
    * @param taskId:
    * @Return: java.util.Map<java.lang.String,java.lang.Object>
    */
    @Select("select USER_ID_ from act_hi_identitylink where TASK_ID_=#{taskId} and PROC_INST_ID_=#{procInstId} limit 1")
    String getActHiIdentitylinkById(@Param("procInstId") String procInstId,@Param("taskId") String taskId);


    // update-begin by wmf at 2022/3/31 15:20 for: 已结束流程回退用到的方法

    /**
     *
     * @Descripton： 运行时流程执行实例表添加记录
     * @author wmf
     * @date 2022/3/31 21:42
     * @param actRuExecution
     */
    @Insert("INSERT INTO ACT_RU_EXECUTION (ID_,	REV_,PROC_INST_ID_,BUSINESS_KEY_," +
            "PROC_DEF_ID_,ACT_ID_,IS_ACTIVE_,IS_CONCURRENT_,IS_SCOPE_,IS_EVENT_SCOPE_," +
            "SUSPENSION_STATE_,CACHED_ENT_STATE_,NAME_)" +
            " values (#{id},1,#{procInstId},#{busKey}," +
            "	#{processId},#{taskKey},1,0,1,0,1,2,#{title})")
    void insertActRuExecution(Map<String, Object> actRuExecution);

    /**
     * @Descripton：将历史流程实例表的endTime设为空值 
     * @author wmf 
     * @date 2022/3/31 21:47
     * @param procInstId
     */
    @Update("update act_hi_procinst set end_time_ = null where proc_inst_id_ = #{procInstId} ")
    void updateActHiProcInstEndtime(@Param("procInstId") String procInstId);

    /**
     * @Descripton：删除待办任务表最后一个节点审批记录 
     * @author wmf 
     * @date 2022/3/31 22:34
     * @param procInstId
     * @param taskId
     */
    @Delete("delete from act_z_task t where t.proc_inst_id = #{procInstId} and t.task_id = #{taskId}")
    void delActZTaskByTaskIdProcInstId(@Param("procInstId") String procInstId, @Param("taskId") String taskId);

    // update-end by wmf at 2022/3/31 15:20 for: 已结束流程回退的方法
    
}
